package commands;

import event.ListMatchsReplyEventArgs;
import game.Match;
import game.MatchStatistics;

import java.util.ArrayList;
import java.util.List;

import messages.ListMatchsReplyMessage;
import messages.Message;
import user.User;
import core.GameManager;


/**
 * The ListPlayers command.
 * 
 * This command will handle ListPlayers messages.
 */
public class ListMatchsCommand extends Command {

	
	public ListMatchsCommand(Message message, User user) {
		super(message, user);
	}
	
	
	@Override
	public void execute() {
		GameManager gameManager = this.controller.getGameManager();
		List<MatchStatistics> listAux = new ArrayList<MatchStatistics>(); 
		
		synchronized(gameManager) {
			for (Match match : gameManager.getActiveMatchs()) {
				listAux.add(new MatchStatistics(match));
			}
		}
			
		ListMatchsReplyMessage replyMsg = new ListMatchsReplyMessage(listAux);	
						
		// Fires the ListMatchs event.
		this.controller.fireListMatchsReplyEvent(new ListMatchsReplyEventArgs(this.user.getLogin(), replyMsg));
	}
}
